package jmine.tec.web.wicket.component.table.resource;

import java.util.List;

import jmine.tec.services.api.export.ExportActor;
import jmine.tec.web.wicket.BeanReference;

import org.apache.wicket.markup.html.WebResource;
import org.apache.wicket.protocol.http.WebResponse;

/**
 * {@link WebResource} para exportação de entidades via serviço.
 * 
 * @author Rafael Volpato
 * @date Aug 8, 2011
 */
public final class ServiceXLSWebResource extends WebResource {

    private final BeanReference<? extends ExportActor> exportActorRef;

    private String filename;

    private ServiceXLSResourceStream resourceStream;

    /**
     * Construtor.
     * 
     * @param exportActorRef referencia para o bean de exportação
     * @param filename nome do arquivo
     */
    public ServiceXLSWebResource(BeanReference<? extends ExportActor> exportActorRef, String filename) {
        this.exportActorRef = exportActorRef;
        this.filename = filename;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ServiceXLSResourceStream getResourceStream() {
        if (this.resourceStream == null) {
            this.resourceStream = new ServiceXLSResourceStream(this.exportActorRef);
        }
        return this.resourceStream;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void setHeaders(WebResponse response) {
        super.setHeaders(response);
        response.setContentType("application/vnd.ms-excel");
        response.setAttachmentHeader(this.filename + ".xls");
    }

    /**
     * @param result the result to set
     */
    public void setResult(List<?> result) {
        this.getResourceStream().setTargets(result);
    }
}
